package simu.tools;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimerTask;

public class SimulateSensor extends TimerTask{
	
	private String sensor, PATH;
		
	public SimulateSensor(String sensor, String PATH) {
		this.sensor = sensor;
		this.PATH = PATH;
	}

	public void run() {
		try {
			start(this.sensor, this.PATH);
		} catch (IOException e) {
			e.printStackTrace();
		}	
	}

	public void start(String sensor, String subfolder) throws IOException{
		if (sensor.equals("audio")){
			DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			Calendar cal = Calendar.getInstance();
			File file = new File(PATH+"/"+dateFormat.format(cal.getTime())+".wav");
			try{
				BufferedWriter bw = new BufferedWriter(new FileWriter(file));			
				for (int i=0; i < 100; i++){
					bw.write("Fila numero " + i + "\n");
				}
				bw.close();
			} catch (IOException ioe){
				//ioe.printStackTrace();
			}
		}
		else if (sensor.equals("video")){
			DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			Calendar cal = Calendar.getInstance();
			File file = new File(PATH+"/"+dateFormat.format(cal.getTime())+".avi");
			String text = "video file sound test.";
			try{
				BufferedWriter bw = new BufferedWriter(new FileWriter(file));			
				bw.write(text);			
				bw.close();
			} catch (IOException ioe){
				//ioe.printStackTrace();
			}
			
		}
		else if (sensor.equals("picture")){
			DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
			Calendar cal = Calendar.getInstance();
			File file = new File(PATH+"/"+dateFormat.format(cal.getTime())+".jpg");
			try{
				BufferedWriter bw = new BufferedWriter(new FileWriter(file));
				for (int i=0; i < 10; i++){
					bw.write("Fila numero " + i + "\n");
				}			
				bw.close();
			} catch (IOException ioe){
				//ioe.printStackTrace();
			}
		}
		else
			throw new IllegalArgumentException(sensor+": is not a valid argument.");
	}


	
}
